package com.ruoyi.framework.shiro.service;

import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.BusDeptApply;
import com.ruoyi.system.mapper.BusDeptApplyMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;

@Service
public class BusDeptApplyService {

	    @Autowired
	    private BusDeptApplyMapper busDeptApplyMapper;
	    
	    @Autowired
	    private ISysDeptService deptService;
	    
	    @Autowired
	    private ISysUserService userService;
	    
	    @Autowired
	    private SysPasswordService passwordService;
	    
	    
		@Transactional
		public int reviewedBusDeptApplyById(Long applyId,boolean pass,String message) {
			BusDeptApply apply = busDeptApplyMapper.selectBusDeptApplyById(applyId);
			Assert.notNull(apply,"申请ID无效");
			//修改审核状态
			if(pass) {
				apply.setStatus(Constants.APPLY_SUCCESS_REVIEWED);
			}else {
				apply.setStatus(Constants.APPLY_FAIL_REVIEWED);
				apply.setAuditRemark(message);
			}
			apply.setAuditUserId(ShiroUtils.getSysUser().getUserId());
			apply.setAuditUserName(ShiroUtils.getLoginName());
			apply.setAuditTime(DateUtils.getNowDate());
			this.busDeptApplyMapper.updateBusDeptApply(apply);
			
			SysDept dept = new SysDept();
			dept.setParentId(0L);
			dept.setCreateBy(ShiroUtils.getLoginName());
			dept.setCreateTime(DateUtils.getNowDate());
			dept.setDelFlag("0");
			dept.setDeptName(apply.getDeptName());
			dept.setEmail(apply.getLagalEmail());
			dept.setLeader(apply.getLegalName());
			dept.setOrderNum("99");
			//新建企业部门信息
			if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
	        {
	            throw new IllegalArgumentException("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
	        }
			
			deptService.insertDept(dept);
			//将默认企业角色与企业部门绑定
			
			//this.roleService.
			//COMPANY_MANAGER_ROLE_CODE
			
			//新建用户信息
			SysUser sysUser = new SysUser();
			sysUser.setCreateBy(ShiroUtils.getLoginName());
			sysUser.setCreateTime(DateUtils.getNowDate());
			sysUser.setDelFlag("0");
			sysUser.setDeptId(dept.getDeptId());
			sysUser.setEmail(apply.getLagalEmail());
			sysUser.setPhonenumber(apply.getLegalPhone());
			sysUser.setLoginName(apply.getLegalPhone());
			sysUser.setUserName(apply.getLegalName());
			sysUser.setPassword(apply.getLegalPhone().substring(apply.getLegalPhone().length()-4, apply.getLegalPhone().length()));
			sysUser.setStatus("0");
			sysUser.setUserType("01");
			
			sysUser.setRoleId(Constants.COMPANY_MANAGER_ROLE_CODE);
			
			if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(sysUser.getLoginName())))
		        {
		           throw new IllegalArgumentException("该法人账号已存在");
		        }
			sysUser.setPwdUpdateDate(DateUtils.getNowDate());
			sysUser.setSalt(ShiroUtils.randomSalt());
			sysUser.setPassword(passwordService.encryptPassword(sysUser.getLoginName(), sysUser.getPassword(), sysUser.getSalt()));
            userService.registerUser(sysUser);
			
			//将用户与默认企业角色绑定
            userService.insertUserAuth(sysUser.getUserId(), new Long[] {Constants.COMPANY_MANAGER_ROLE_CODE});
			
			return 1;
		}
	    
}
